//
//  MeetControlViewController.m
//  ideashareDemo
//
//  Created by huawei on 2021/1/25.
//  Copyright © 2021 huawei. All rights reserved.
//

#import "MeetControlViewController.h"
#import <objc/runtime.h>
#import <IdeaShare_SDK/IdeaShareServiceController.h>
#import <IdeaShare_SDK/IdeaShareDef.h>
#import <IdeaShare_SDK/IdeaShareEventHandler.h>
#import "ViewController.h"

BOOL inMeeting = NO;
extern bool isSharing;
@interface MeetControlViewController ()

@property BOOL isChairman;

@property IdeaShareServiceController *mShareController;
// 组件
@property UISwitch *micSwitch;
@property UISwitch *cameraSwitch;
@property UISlider *volumeSlider;
@property UIButton *muteBtn;
@property UIButton *leaveMeet;
@property UISwitch *endMeetSwitch;
@property UILabel *endMeetLbl;
@end

@implementation MeetControlViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //获取单例
    _mShareController = [IdeaShareServiceController shareInstance];
//    self.mShareController.notifyDelegate = self;
    
    self.view.backgroundColor = [UIColor whiteColor];
    CGFloat viewWidth = self.view.frame.size.width;
    CGFloat viewHeight = self.view.frame.size.height;
    
    //1.麦克风开关标签
    UILabel *micLbl = [[UILabel alloc]initWithFrame:CGRectMake((viewWidth-100)/3, viewHeight/5, 100, 20)];
    micLbl.text = @"麦克风";
    [self.view addSubview:micLbl];
    
    //2.麦克风开关按钮
    _micSwitch = [[UISwitch alloc]initWithFrame:CGRectMake((viewWidth-100)/3, viewHeight/4, 0, 0)];
    //查询当前麦克风状态
    [_micSwitch addTarget:self action:@selector(micSwitchChg:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_micSwitch];
    
    //3.摄像头开关标签
    UILabel *cameraLbl = [[UILabel alloc]initWithFrame:CGRectMake(viewWidth*2/3, viewHeight/5, 100, 20)];
    cameraLbl.text = @"摄像头";
    [self.view addSubview:cameraLbl];
    
    //4.摄像头开关按钮
    _cameraSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(viewWidth*2/3, viewHeight/4, 0, 0)];
    //查询当前摄像头状态
    [_cameraSwitch addTarget:self action:@selector(cameraSwitchChg:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_cameraSwitch];
    
    //5.音量滑块
    _volumeSlider = [[UISlider alloc]initWithFrame:CGRectMake((viewWidth-100)/4, viewHeight/3, (viewWidth-100)*2/3 , 20)];
    //设置滑块最左边的图片
    UIImage *imgVolume = [UIImage imageNamed:@"volume02"];
    _volumeSlider.minimumValueImage = imgVolume;
    //设置滑块最大值
    _volumeSlider.maximumValue = 21;
    //注册事件监听
    [_volumeSlider addTarget:self action:@selector(volumeChange:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_volumeSlider];
    
    //6.静音按钮
    _muteBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _muteBtn.frame = CGRectMake(viewWidth-100, viewHeight/3, 50, 20);
    [_muteBtn.layer setMasksToBounds:YES];
    [_muteBtn.layer setCornerRadius:10.0];
    [_muteBtn.layer setBorderWidth:1.0];
    //创建静音按钮与音量滑块的关联关系
    objc_setAssociatedObject(_muteBtn, @"getVolumeSli", _volumeSlider, OBJC_ASSOCIATION_ASSIGN);
    [_muteBtn addTarget:self action:@selector(enableMute:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_muteBtn];
    
    //7.离开会议按钮
    _leaveMeet = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _leaveMeet.frame =CGRectMake((viewWidth-100)/2, viewHeight/2, 130, 40);
    _leaveMeet.enabled = NO;
    //设置按钮字体大小
    _leaveMeet.titleLabel.font = [UIFont systemFontOfSize:20.0];
    [_leaveMeet setTitle:@"离开会议" forState:UIControlStateNormal];
    //设置按钮边框
    [_leaveMeet.layer setMasksToBounds:YES];
    [_leaveMeet.layer setCornerRadius:10.0];
    [_leaveMeet.layer setBorderWidth:1.0];

    [_leaveMeet addTarget:self action:@selector(_leaveMeeting:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_leaveMeet];

    //8.主席结束会议复选按钮
    //调用接口查询当前连接的设备是否是主席
    _endMeetLbl = [[UILabel alloc]initWithFrame:CGRectMake((viewWidth-100)/2, viewHeight/2+50, 100, 30)];
    _endMeetLbl.text = @"结束会议";
    _endMeetLbl.font = [UIFont systemFontOfSize:15];
    _endMeetLbl.textColor = [UIColor redColor];

    _endMeetSwitch = [[UISwitch alloc]initWithFrame:CGRectMake((viewWidth+50)/2, viewHeight/2+50, 0, 0)];
    _endMeetSwitch.on = NO;
    
    // 初始化组件状态
    [_mShareController getRemoteServiceStatus:^(id t){
        RemoteServiceStatus * remoteServiceStatus = (RemoteServiceStatus *)t;
        self->_isChairman = remoteServiceStatus.isChairman;
        dispatch_async(dispatch_get_main_queue(), ^{
        // mic开关状态
        self->_micSwitch.on = remoteServiceStatus.isMute == 1 ? NO : YES;
        // 摄像头开关状态
        if(remoteServiceStatus.isBase == 0){
            NSLog(@"设备有摄像机，状态为：%d",remoteServiceStatus.isCameraMute);
            self->_cameraSwitch.enabled = YES;
            self->_cameraSwitch.on = remoteServiceStatus.isCameraMute == 1? NO:YES;
        }else{
            self->_cameraSwitch.enabled = NO;
        }
        // 音量状态
        NSLog(@"获取到的音量为：%d",remoteServiceStatus.volume);
        self->_volumeSlider.value = remoteServiceStatus.volume;
        // 静音状态
        UIImage *imgMute = [UIImage imageNamed:@"mute01"];
        UIImage *imgVolume = [UIImage imageNamed:@"volume02"];
        if(remoteServiceStatus.isSpeakerMute == 1){
            self->_volumeSlider.minimumValueImage = imgMute;
            [self->_muteBtn setTitle:@"取消" forState:UIControlStateNormal];
        }else{
            self->_volumeSlider.minimumValueImage = imgVolume;
            [self->_muteBtn setTitle:@"静音" forState:UIControlStateNormal];
        }
        //离开会议按钮状态
            if(inMeeting){
                self->_leaveMeet.enabled = YES;
                NSLog(@"进入会议控制页面，当前会场是否为主席：%d",self->_isChairman);
                if(self->_isChairman){
                    [self.view addSubview:self->_endMeetLbl];
                    [self.view addSubview:self->_endMeetSwitch];
                }
            }
        });
    }];
    
}

//麦克风开关点击事件处理
- (void) micSwitchChg:(UISwitch*)sw{
    if(sw.on == NO){
        NSLog(@"麦克风开关打开！！！");
        [_mShareController setRemoteMicMute:1 callback:^(id t){} ];
    } else{
        NSLog(@"麦克风开关关闭！！！");
        [_mShareController setRemoteMicMute:0 callback:^(id t){} ];
    }
}

//摄像头开关点击事件处理
- (void) cameraSwitchChg:(UISwitch*)sw{
    if(sw.on == NO){
        NSLog(@"摄像头开关打开！！！");
        [_mShareController setRemoteCameraMute:1 callback:^(id t){}];
    
    } else{
        NSLog(@"摄像头开关关闭！！！");
        [_mShareController setRemoteCameraMute:0 callback:^(id t){}];
    }
}

- (void) volumeChange:(UISlider*)sli{
    NSLog(@"用户修改音量大小！！！！");
    [_mShareController setSpeakerVolume:sli.value callback:^(id t){}];
}

- (void) enableMute:(UISlider*)btn{
    //获取跟静音按钮关联的滑块对象
    UIImage *imgMute = [UIImage imageNamed:@"mute01"];
    UIImage *imgVolume = [UIImage imageNamed:@"volume02"];
    if([_volumeSlider.minimumValueImage isEqual:(imgVolume)])
    {
        NSLog(@"用户点击静音按钮!!!!");
        int enableSpeakerMute = 1;
        [_mShareController setRemoteSpeakerMute:enableSpeakerMute callback:^(id t){
            dispatch_async(dispatch_get_main_queue(), ^{
                self->_volumeSlider.minimumValueImage = imgMute;
                [self->_muteBtn setTitle:@"取消" forState:UIControlStateNormal];
            });
        } ];
    } else{
        NSLog(@"用户取消静音!!!!");
        int disableSpeakerMute = 0;
        [_mShareController setRemoteSpeakerMute:disableSpeakerMute callback:^(id t){
            dispatch_async(dispatch_get_main_queue(), ^{
                self->_volumeSlider.minimumValueImage = imgVolume;
                [self->_muteBtn setTitle:@"静音" forState:UIControlStateNormal];
            });
        } ];
    }
}

- (void) _leaveMeeting:(UIButton*)btn{
    //主席离开
    NSLog(@"离开会议，当前主席状态为：%d ，结束会议开关为：%d",_isChairman,_endMeetSwitch.on);
    if(_isChairman && _endMeetSwitch.on){
        NSLog(@"主席结束会议！！！");
        [_mShareController finishConference:^(id t){
            //结束会议后将按钮置为无效
            dispatch_async(dispatch_get_main_queue(), ^{
                self->_leaveMeet.enabled = NO;
                [self->_endMeetSwitch removeFromSuperview];
                [self->_endMeetLbl removeFromSuperview];
            });
        }];
    }
    else{
        NSLog(@"离开会议！！！");
        [_mShareController leaveConference:^(id t){
            //离开会议后将按钮置为无效
            dispatch_async(dispatch_get_main_queue(), ^{
                self->_leaveMeet.enabled = NO;
                [self->_endMeetSwitch removeFromSuperview];
                [self->_endMeetLbl removeFromSuperview];
            });
        }];
    }
    
}

- (void) OnDevConfStatusChangeNotify : (int) confStatus{
    dispatch_async(dispatch_get_main_queue(), ^{
        self->_leaveMeet.enabled = confStatus==1 ? YES : NO;
		if(confStatus == 0 && self->_isChairman == 1){
            [self->_endMeetSwitch removeFromSuperview];
            [self->_endMeetLbl removeFromSuperview];
        }
    });
    
}
- (void) OnRemoteMicStatusNotify : (int) isMute{
    dispatch_async(dispatch_get_main_queue(), ^{
        self->_micSwitch.on = isMute==0 ? YES : NO;
    });
    
}
- (void) OnRemoteCameraStatusNotify : (int) isCameraMute{
    dispatch_async(dispatch_get_main_queue(), ^{
        self->_cameraSwitch.on = isCameraMute==0 ? YES : NO;
    });
}
- (void) OnRemoteSpeakerStatusNotify : (int) isSpeakerMute{
    dispatch_async(dispatch_get_main_queue(), ^{
        UIImage *imgMute = [UIImage imageNamed:@"mute01"];
        UIImage *imgVolume = [UIImage imageNamed:@"volume02"];
        if(isSpeakerMute == 1){
            self->_volumeSlider.minimumValueImage = imgMute;
            [self->_muteBtn setTitle:@"取消" forState:UIControlStateNormal];
        }else{
            self->_volumeSlider.minimumValueImage = imgVolume;
            [self->_muteBtn setTitle:@"静音" forState:UIControlStateNormal];
        }
    });
}
- (void) OnRemoteChairManStatusNotify : (int) isChairMan{
    _isChairman = isChairMan;
    dispatch_async(dispatch_get_main_queue(), ^{
        if(isChairMan == 1){
            [self.view addSubview:self->_endMeetLbl];
            [self.view addSubview:self->_endMeetSwitch];
        }else{
            [self->_endMeetLbl removeFromSuperview];
            [self->_endMeetSwitch removeFromSuperview];
        }
    });
}
- (void) OnRemoteSpeakerVolumeStatusNotify : (int) speakerVolume{
    dispatch_async(dispatch_get_main_queue(), ^{
        self->_volumeSlider.value = speakerVolume;
    });
}


@end
